<%--
  访问地址： http://localhost:8099/myJspDemo/mainIframe.jsp?micro_route=hello
  访问地址： http://localhost:8099/myJspDemo/mainIframe.jsp?micro_route=user
  访问地址： http://localhost:8099/myJspDemo/mainIframe.jsp?micro_route=test
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
  String appUrl = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath();
%>
<!DOCTYPE html>
<html>
<head>
  <title>子应用JSP-iframe模式</title>
  <link href="<%= appUrl %>/css/style.css" rel="stylesheet">
  <%--  <base href="${pageContext.request.contextPath}/">--%>
  <base href="<%= appUrl %>/">
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<script>
  // 地址中参数 '?userid=5&usercode=zhangsan&jssdk=http://124.70.24.169:3777/portal-site/lib/workflow-sdk.min.js'
  if(window.location.href.includes('jssdk')) {
    const jssdk = getQueryString('jssdk')
    setScript(jssdk)
  }

  function getQueryString(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    var r = window.location.search.substring(1).match(reg);
    if (r != null) {
      return decodeURIComponent(r[2]);
    }
    return null;
  }

  // 初始加载 sdk script
  function setScript(url) {
    let script = document.createElement('script')
    script.type = 'text/javascript'
    script.src = url
    document.body.appendChild(script)
    var urlObj = new URL(url)
    window.JssdkOrigin = urlObj.origin
    script.onload = function(){
      callback();
    }
  }

  // 加载 sdk之后回调函数
  function callback() {
    if (window.JssdkOrigin) {
      window.webJssdk = new window.WorkFlowSDK({
        targetOrigin: window.JssdkOrigin,
        callbacks: {
          getDataCallback: getDataCallbackFun
        }
      })

      // 发送事件
      window.webJssdk.emit('getData', { callbackid: 'getDataCallback' })
      // 监听事件
      window.webJssdk.on('testCallback', testCallbackFun)

      function getDataCallbackFun(args) {
        console.log('[子应用 myJspDemo] - getDataCallback:',args)
      }
      function  testCallbackFun(args) {
        console.log('[子应用 myJspDemo] - testCallbackFun:',args)
        window.webJssdk.postMessage({
          callbackid: args.callbackid,
          data: { code: 200, msg: '执行成功!' }
        })
      }
    }
  }
</script>

<%
  String microRoute = request.getParameter("micro_route");
  if(microRoute == null) microRoute = "index.jsp";
  System.out.println("micro_route："+microRoute);

  switch(microRoute) {
    case "hello":
      //request.getRequestDispatcher("/hello-servlet").include(request, response);
      microRoute = "/hello-servlet";
      break;
    case "user":
      //request.getRequestDispatcher("/user-servlet").include(request, response);
      microRoute = "/user-servlet";
      break;
    default:
      // 显示默认内容
      //out.println("<h1>Welcome to JSP App</h1>");
      //out.println("<p>Current route: " + microRoute + "</p>");
      microRoute = "test.jsp";
      break;
  }
%>
<jsp:include page="<%= microRoute %>"/>
</body>
</html>